package leetcode;

/**
 * @program: datastructureandalogorithm
 * @description:
 * @author: hmx
 * @create: 2021-12-08 00:45
 **/
public class LeetCode689 {

    public int[] maxSumOfThreeSubarrays(int[] nums, int k) {
        int[] res = new int[3];
        int sum1 = 0;
        int maxSum1 = 0;
        int maxIndex1 = 0;
        int sum2 = 0;
        int maxSum2 = 0;
        int max2Index1 = 0;
        int max2Index2 = 0;
        int sum3 = 0;
        int maxSum3 = 0;
        for (int i = 2 * k; i < nums.length; i++) {
            sum1 += nums[i - 2 * k];
            sum2 += nums[i - k];
            sum3 += nums[i];

            if (i >= 3 * k - 1) {
                if (sum1 > maxSum1) {
                    maxSum1 = sum1;
                    maxIndex1 = i - 3 * k + 1;
                }

                if (sum2 + maxSum1 > maxSum2) {
                    maxSum2 = sum2 + maxSum1;
                    max2Index1 = maxIndex1;
                    max2Index2 = i - 2 * k + 1;
                }

                if (sum3 + maxSum2 > maxSum3) {
                    maxSum3 = sum3 + maxSum2;
                    res[0] = max2Index1;
                    res[1] = max2Index2;
                    res[2] = i - k + 1;
                }

                sum1 -= nums[i - 3 * k + 1];
                sum2 -= nums[i - 2 * k + 1];
                sum3 -= nums[i - k + 1];
            }
        }
        return res;
    }

}
